package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.SysCheck;
import com.ruoyi.system.service.ISysCheckService;
import com.ruoyi.web.controller.common.CommonController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 验收阶段
 * @Author ：zhaoguanyu
 * @Date ：Created in 2021/12/18 17:28
 */
@Api(tags = "验收阶段")
@RestController
@RequestMapping("/system/check")
public class SysCheckController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private ISysCheckService sysCheckService;

    /**
     *提交验收报告
     * @Author zhaoguanyu
     * @Date 13:59 2021/12/21
     */
    @ApiOperation(value = "提交验收报告")
    @PreAuthorize("@ss.hasPermi('system:check:submit')")
    @Log(title = "验收阶段", businessType = BusinessType.IMPORT)
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            //文件中文名称
            String name = file.getOriginalFilename();
            SysCheck sysCheck = new SysCheck();
            sysCheck.setFileName(name);
            sysCheck.setFilePath(filePath);
            sysCheck.setUrl(url);
            sysCheck.setOnlyName(fileName);
            Long id = sysCheckService.insertFileUpload(sysCheck);
            ajax.put("id",id);
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     *提交支付凭证图片
     * @Author zhaoguanyu
     * @Date 13:59 2021/12/21
     */
    @ApiOperation(value = "提交验收报告")
    @PreAuthorize("@ss.hasPermi('system:check:submit')")
    @Log(title = "验收阶段", businessType = BusinessType.IMPORT)
    @PostMapping("/uploadPicture")
    public AjaxResult uploadPicture(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            SysCheck sysCheck = new SysCheck();
            sysCheck.setpName(fileName);
            sysCheck.setFilePath(filePath);
            sysCheck.setpUrl(url);
            Long id = sysCheckService.uploadPicture(sysCheck);
            ajax.put("id",id);
            ajax.put("url", url);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     *提交验收报告信息
     * @Author zhaoguanyu
     * @Date 13:59 2021/12/21
     */
    @ApiOperation(value = "提交验收报告信息")
    @PreAuthorize("@ss.hasPermi('system:check:submitInformation')")
    @Log(title = "验收阶段",businessType = BusinessType.INSERT)
    @PostMapping("/checkAdd")
    public AjaxResult add(@RequestBody SysCheck sysCheck){
        sysCheck.setFileName(sysCheck.getOnlyName());
        return toAjax(sysCheckService.updateCheckInformation(sysCheck));
    }

    /**
     *提交支付进度款信息
     * @Author zhaoguanyu
     * @Date 13:59 2021/12/31
     */
    @ApiOperation(value = "提交验收报告信息")
    @PreAuthorize("@ss.hasPermi('system:check:submitInformation')")
    @Log(title = "验收阶段",businessType = BusinessType.INSERT)
    @PostMapping("/payAdd")
    public AjaxResult payAdd(@RequestBody SysCheck sysCheck){
        return toAjax(sysCheckService.updatePayInformation(sysCheck));
    }

}